//
//  LXPermission.swift
//  LXPermissionAuth
//
//  Created by yoctech on 2021/8/18.
//

import Foundation
import UIKit

@objc public protocol LXPermissionProtocol {
    static func auth(autoRequestIfNot:Bool, clouser: @escaping LXAuthClouser)
}

@objc public enum LXAuthStatu:Int {
    
    case notDetermined = 0

    case denied = 1

    case restricted = 2

    case authorized = 3
    
    case other = 4
}

public typealias LXAuthClouser = ((Bool,LXAuthStatu)->())

@objc public class LXPermissionHelper: NSObject {
    
    @objc public class func lxf_openSetting() {
        DispatchQueue.main.async {
            guard let url = URL(string:UIApplication.openSettingsURLString) else {return}
            if UIApplication.shared.canOpenURL(url) {
                if #available(iOS 10.0, *) {
                    UIApplication.shared.open(url, options: [:], completionHandler: nil)
                } else {
                    UIApplication.shared.openURL(url)
                }
            }
        }
    }
    
    @objc public class func showSetAlert(msg:String, to vc:UIViewController?){
        let alert = UIAlertController.init(title: "权限设置", message: msg, preferredStyle: .alert)
        let escAc = UIAlertAction.init(title: "取消", style: .cancel, handler: nil)
        let goAc = UIAlertAction.init(title: "去设置", style: .destructive) { _ in
            self.lxf_openSetting()
        }
        alert.addAction(escAc)
        alert.addAction(goAc)
        vc?.present(alert, animated: true, completion: nil)
    }

}

